import 'dart:convert';

import 'package:pt_manage/model/site_user_info.dart';

/// id : 1
/// name : "M-Team"
/// timezoneOffset : "+0800"
/// description : "M-Team"
/// url : "https://xp.m-team.cc/"
/// icon : "https://xp.m-team.cc/favicon.ico"
/// tags : ["影视","综合","成人"]
/// host : "xp.m-team.cc"
/// schema : "mTorrent"
/// cdn : ["https://kp.m-team.cc/","https://xp.m-team.cc/","https://ap.m-team.cc/"]
/// apiCdn : ["https://api.m-team.io/","https://api.m-team.cc/","https://api2.m-team.cc/","https://mtapi.m-team.cc/"]
/// tokenRequired : true
/// formerHosts : ["xp.m-team.io","pt.m-team.cc","kp.m-team.cc","tp.m-team.cc"]
/// uploadRateLimit : 0
/// passKey : ""
/// authToken : ""

class Site {
  Site(
      {int? id,
      String? name,
      String? timezoneOffset,
      String? description,
      String? url,
      String? icon,
      List<String>? tags,
      String? host,
      String? schema,
      List<String>? cdn,
      List<String>? apiCdn,
      bool? tokenRequired,
      List<String>? formerHosts,
      int? uploadRateLimit,
      String? passKey,
      String? authToken,
      String? cookie,
      DateTime? createTime,
      DateTime? updateTime}) {
    _id = id;
    _name = name;
    _timezoneOffset = timezoneOffset;
    _description = description;
    _url = url;
    _icon = icon;
    _tags = tags;
    _host = host;
    _schema = schema;
    _cdn = cdn;
    _apiCdn = apiCdn;
    _tokenRequired = tokenRequired;
    _formerHosts = formerHosts;
    _uploadRateLimit = uploadRateLimit;
    _passKey = passKey;
    _authToken = authToken;
    _cookie = cookie;
    _createTime = createTime;
    _updateTime = updateTime;
  }

  Site.fromJson(dynamic json) {
    _id = json['id'];
    _name = json['name'];
    _timezoneOffset = json['timezoneOffset'];
    _description = json['description'];
    _url = json['url'];
    _icon = json['icon'];
    _tags = json['tags'] != null ? jsonDecode(json['tags']).cast<String>() : [];
    _host = json['host'];
    _schema = json['schema'];
    _cdn = json['cdn'] != null ? jsonDecode(json['cdn']).cast<String>() : [];
    _apiCdn =
        json['apiCdn'] != null ? jsonDecode(json['apiCdn']).cast<String>() : [];
    _tokenRequired = json['tokenRequired'];
    _formerHosts = json['formerHosts'] != null
        ? jsonDecode(json['formerHosts']).cast<String>()
        : [];
    _uploadRateLimit = json['uploadRateLimit'];
    _passKey = json['passKey'];
    _authToken = json['authToken'];
    _cookie = json['cookie'];
    _createTime = json['createTime'];
    _updateTime = json['updateTime'];
  }

  int? _id;
  String? _name;
  String? _timezoneOffset;
  String? _description;
  String? _url;
  String? _icon;
  List<String>? _tags;
  String? _host;
  String? _schema;
  List<String>? _cdn;
  List<String>? _apiCdn;
  bool? _tokenRequired;
  List<String>? _formerHosts;
  int? _uploadRateLimit;
  String? _passKey;
  String? _authToken;
  String? _cookie;
  DateTime? _createTime;
  DateTime? _updateTime;

  SiteUserInfo? _userInfo;

  Site copyWith({
    int? id,
    String? name,
    String? timezoneOffset,
    String? description,
    String? url,
    String? icon,
    List<String>? tags,
    String? host,
    String? schema,
    List<String>? cdn,
    List<String>? apiCdn,
    bool? tokenRequired,
    List<String>? formerHosts,
    int? uploadRateLimit,
    String? passKey,
    String? authToken,
    String? cookie,
    DateTime? createTime,
    DateTime? updateTime,
  }) =>
      Site(
        id: id ?? _id,
        name: name ?? _name,
        timezoneOffset: timezoneOffset ?? _timezoneOffset,
        description: description ?? _description,
        url: url ?? _url,
        icon: icon ?? _icon,
        tags: tags ?? _tags,
        host: host ?? _host,
        schema: schema ?? _schema,
        cdn: cdn ?? _cdn,
        apiCdn: apiCdn ?? _apiCdn,
        tokenRequired: tokenRequired ?? _tokenRequired,
        formerHosts: formerHosts ?? _formerHosts,
        uploadRateLimit: uploadRateLimit ?? _uploadRateLimit,
        passKey: passKey ?? _passKey,
        authToken: authToken ?? _authToken,
        cookie: cookie ?? _cookie,
        createTime: createTime ?? _createTime,
        updateTime: updateTime ?? _updateTime,
      );

  int? get id => _id;

  String? get name => _name;

  String? get timezoneOffset => _timezoneOffset;

  String? get description => _description;

  String? get url => _url;

  String? get icon => _icon;

  List<String>? get tags => _tags;

  String? get host => _host;

  String? get schema => _schema;

  List<String>? get cdn => _cdn;

  List<String>? get apiCdn => _apiCdn;

  bool? get tokenRequired => _tokenRequired;

  List<String>? get formerHosts => _formerHosts;

  int? get uploadRateLimit => _uploadRateLimit;

  String? get passKey => _passKey;

  String? get authToken => _authToken;
  
  String? get cookie => _cookie;
  
  DateTime? get createTime => _createTime;
  
  DateTime? get updateTime => _updateTime;

  SiteUserInfo? get userInfo => _userInfo;

  set id(int? id) => _id = id;

  set name(String? name) => _name = name;

  set timezoneOffset(String? timezoneOffset) =>
      _timezoneOffset = timezoneOffset;

  set description(String? description) => _description = description;

  set url(String? url) => _url = url;

  set icon(String? icon) => _icon = icon;

  set tags(List<String>? tags) => _tags = tags;

  set host(String? host) => _host = host;

  set schema(String? schema) => _schema = schema;

  set cdn(List<String>? cdn) => _cdn = cdn;

  set apiCdn(List<String>? apiCdn) => _apiCdn = apiCdn;

  set tokenRequired(bool? tokenRequest) => _tokenRequired = tokenRequest;

  set formerHosts(List<String>? formerHosts) => _formerHosts = formerHosts;

  set uploadRateLimit(int? uploadRateLimit) =>
      _uploadRateLimit = uploadRateLimit;

  set passKey(String? passKey) => _passKey = passKey;

  set authToken(String? authToken) => _authToken = authToken;
  
  set cookie(String? cookie) => _cookie = cookie;
  
  set createTime(DateTime? createTime) => _createTime = createTime;
  
  set updateTime(DateTime? updateTime) => _updateTime = updateTime;

  set userInfo(SiteUserInfo? userInfo) => _userInfo = userInfo;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = _id;
    map['name'] = _name;
    map['timezoneOffset'] = _timezoneOffset;
    map['description'] = _description;
    map['url'] = _url;
    map['icon'] = _icon;
    map['tags'] = jsonEncode(_tags ?? []);
    map['host'] = _host;
    map['schema'] = _schema;
    map['cdn'] = jsonEncode(_cdn ?? []);
    map['apiCdn'] = jsonEncode(_apiCdn ?? []);
    map['tokenRequired'] = _tokenRequired;
    map['formerHosts'] = jsonEncode(_formerHosts ?? []);
    map['uploadRateLimit'] = _uploadRateLimit;
    map['passKey'] = _passKey;
    map['authToken'] = _authToken;
    map['cookie'] = _cookie;
    map['createTime'] = _createTime;
    map['updateTime'] = _updateTime;
    return map;
  }
}
